package ${package}.commons;

import java.io.Serializable;
import java.util.List;

/**
 * Page object for pagination, no page calculation.
 * 
 * @author Ke
 */
@SuppressWarnings("serial")
public class Page<T> implements Serializable {
	public static int DEFAULT_PAGE_SIZE = 20;

	// how many record in this page
	private int fetchSize;
	private int offset;
	private int currentPageNo;
	private int totalCount;
	private int pageSize;
	// record for the current page, usually is an List.
	private List<T> data;

	private int pageCount;

	public Page() {
		this.pageSize = DEFAULT_PAGE_SIZE;
	}

	public Page(int pageSize) {
		this.pageSize = pageSize;
	}

	public Page(int currentPageNo, int totalCount, List<T> data) {
		this.currentPageNo = currentPageNo;
		this.totalCount = totalCount;
		this.data = data;
	}

	public Page(int currentPageNo, int totalCount) {
		this(currentPageNo, totalCount, null);

	}

	public int getPageCount() {
		pageCount = totalCount / this.pageSize;
		if (totalCount % this.pageSize == 0)
			return pageCount;
		else
			return pageCount + 1;

	}

	public int getOffset() {
		offset = currentPageNo * this.pageSize;
		return offset;
	}

	public int getFetchSize() {
		if ((currentPageNo + 1) * this.pageSize > this.totalCount) {

			fetchSize = totalCount - currentPageNo * this.pageSize;
		} else {
			fetchSize = this.pageSize;
		}

		return fetchSize;
	}

	public int getCurrentPageNo() {
		return currentPageNo;
	}

	public void setCurrentPageNo(int currentPageNo) {
		this.currentPageNo = currentPageNo;
	}

	public long getTotalCount() {
		return totalCount;
	}

	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}

	public List<T> getData() {
		return data;
	}

	public void setData(List<T> data) {
		this.data = data;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

}
